<?php

/*
  this file below include file configurate with database
 */
require_once('../libraryfiles/config.php');
$foregetpass = isset($_POST['email']) ? $_POST['email'] : '';
$pass_and_confirm = isset($_POST['txtpassword']) ? $_POST['txtpassword'] : '';
$txtconfirm = isset($_POST['txtconfirm']) ? $_POST['txtconfirm'] : '';
/*
  this is condition for validation email when they insert mail,if it is right, so it has done
 */
if ($foregetpass) {
    ValidateEmail($foregetpass);
}
/*
  this is condition for performing function input new password for login
 */ else if ($pass_and_confirm) {
    $emailreset = isset($_POST['emailreset']) ? $_POST['emailreset'] : '';
    $activateid = isset($_POST['activateid']) ? $_POST['activateid'] : '';
    if (!empty($txtconfirm) && !empty($pass_and_confirm) && !empty($emailreset)) {
        validate_updatepassword($txtconfirm, $pass_and_confirm, $emailreset, $activateid);
    }
}

/*
  this is function for checking validation emai
 */

function ValidateEmail($email) {
    $getCodGeneration = genRandomString();
    $selet_email = select(Tbluser::Tbluser, array(Tbluser::email, Tbluser::activate, Tbluser::first_name, Tbluser::last_name), array(Tbluser::email => $email));
    $nums = mysql_num_rows($selet_email);
    if ($nums > 0) {
        while ($rows = mysql_fetch_array($selet_email, MYSQL_ASSOC)) {
            $Email = $rows['email'];
        }
        if ($Email == $email) {
            $SET = array(Tbluser::activate => $getCodGeneration);
            $update_activate = update(Tbluser::Tbluser, $SET, array(Tbluser::email => $email));
            $_SESSION['email'] = $Email;
            if ($update_activate == TRUE) {
                /* send email to confirm */
                include_once S_ROOT . '/libraryfiles/SendEmail.php';
                $transport = new SendEmail ();
                $name = $first_name . $last_name;
                $header = 'info.neakporn@gmail.com';
                $subjects = "Psarinternet Reset password";
                $base = "http://" . $_SERVER ["SERVER_NAME"];
                $BodyHeader = "RESET YOUR PASSWORD!";
                $BodyMessage = 'Click here to create new password: <a href="' . base_url . 'account/reset_password.php?confirm=' . $getCodGeneration . '&id=' . $email . '">Reset password!</a> ';
                $transport = $transport->SendMail($name, $email, $base, $header, $subjects, $BodyHeader, $BodyMessage);
                /* end send email to confirm */
                header('location:email_form.php?success=PW_SENT_TO_EMAIL');
                exit();
            }
        } else if ($Email != $email) {
            $_SESSION['ms_erro'] = "<h3 style='color:red; background-color:gray;text-decoration:blink;'>Your Email is not match!</h3>";
            header('location:email_form.php');
            exit();
        }
    } else {
        $_SESSION['ms_erro'] = "<h3 style='color:red; background-color:gray;text-decoration:blink;'>Your Email is not exist!</h3>";
        header('location:email_form.php');
        exit();
    }
}

/*
  this is function for validation new password that they input new password

 */

function validate_updatepassword($txtconfirm, $pass_and_confirm, $emailreset, $activateid) {
    if ($txtconfirm != $pass_and_confirm) {
        $_SESSION['ms_not'] = "<center><h3 style='color:red; background-color:gray;text-decoration:blink;'>Your Password and Confirmpassword is not match!</h3></center>";
        header('location:foreget_password.php?id='.$emailreset.'&confirm='.$activateid.'');
        exit();
    } else if ($txtconfirm == $pass_and_confirm) {
        $update_pass = update(Tbluser::Tbluser, array(Tbluser::user_pass => GenPassword($txtconfirm)), array(Tbluser::email => $emailreset));
        //$update_pass = mysql_query("UPDATE tbluser SET user_pass='" . $confirmpassword . "' WHERE email='" . $YOUR_Email . "'");
        if ($update_pass == TRUE) {
            $base = base_url;
            header('location:'.$base.'login.php?success=PW_RESET_NEW_HAS_RESET');
            exit();
        }
    }
}

?>
